package com.pay.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.pay.dubbo.wx.EnterPrisePaymentServiceFacade;
import com.pay.dubbo.wx.MenuInfoServiceFacade;
import com.pay.dubbo.wx.WxAccessTokenServiceFacade;
import com.pay.dubbo.wx.WxCustomerServiceFacade;
import com.pay.dubbo.wx.WxPosOrderServiceFacade;
import com.pay.dubbo.wx.WxServiceFacade;
import com.pay.dubbo.wx.WxUserInfoServiceFacade;

/**
 * @Description: dubbo基础配置
 * @see: DubboConfig 此处填写需要参考的类
 * @version 2016年8月24日 下午6:21:05
 * @author jian.cao
 */
@Configuration
@PropertySource("classpath:/system.properties")
public class DubboConfig {

	@Value("${dubbo.appName}")
	private String appName;

	// pay_dubbo注册地址
	@Value("${dubbo.address}")
	private String resAddress;

	@Value("${dubbo.registryProtocol}")
	private String registryProtocol;

	@Value("${dubbo.protocolName}")
	private String protocolName;

	@Value("${dubbo.port}")
	private int port;

	@Value("${dubbo.connections}")
	private int connections;

	/**
     * 注入dubbo上下文
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        // 当前应用配置
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(this.appName);
        return applicationConfig;
    }
	
    /**
     * 注入dubbo注册中心配置,基于zookeeper
     * @return
     */
    @Bean(name = "pay") 
    @Primary
    public RegistryConfig payRegConfig() {
        // 连接注册中心配置
    	RegistryConfig registryConfig = new RegistryConfig(resAddress);
		registryConfig.setProtocol(registryProtocol);
		registryConfig.setPort(port);
		registryConfig.setDefault(true);
		registryConfig.setId("pay");
        return registryConfig;
    }

	@Bean
	public ProtocolConfig protocol() {
		ProtocolConfig protocolConfig = new ProtocolConfig(protocolName, port);
		return protocolConfig;
	}

	@Bean
	public MonitorConfig monitor() {
		MonitorConfig monitorConfig = new MonitorConfig();
		monitorConfig.setProtocol("registry");
		return monitorConfig;
	}

	@Bean
	public ProviderConfig provider() {
		ProviderConfig config = new ProviderConfig();
		config.setConnections(connections);
		return config;
	}

    @Bean(name="wxCustomerServiceFacade")
    public ReferenceBean<WxCustomerServiceFacade> wxCustomerServiceFacade() {
        ReferenceBean<WxCustomerServiceFacade> ref = new ReferenceBean<WxCustomerServiceFacade>();
        ref.setInterface(WxCustomerServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    
    @Bean(name="wxAccessTokenServiceFacade")
    public ReferenceBean<WxAccessTokenServiceFacade> wxAccessTokenServiceFacade() {
        ReferenceBean<WxAccessTokenServiceFacade> ref = new ReferenceBean<WxAccessTokenServiceFacade>();
        ref.setInterface(WxAccessTokenServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    
    
    @Bean(name="wxServiceFacade")
    public ReferenceBean<WxServiceFacade> wxServiceFacade() {
        ReferenceBean<WxServiceFacade> ref = new ReferenceBean<WxServiceFacade>();
        ref.setInterface(WxServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    
    @Bean(name="wxUserInfoServiceFacade")
    public ReferenceBean<WxUserInfoServiceFacade> wxUserInfoServiceFacade() {
        ReferenceBean<WxUserInfoServiceFacade> ref = new ReferenceBean<WxUserInfoServiceFacade>();
        ref.setInterface(WxUserInfoServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    
    @Bean(name="menuInfoServiceFacade")
    public ReferenceBean<MenuInfoServiceFacade> menuInfoServiceFacade() {
        ReferenceBean<MenuInfoServiceFacade> ref = new ReferenceBean<MenuInfoServiceFacade>();
        ref.setInterface(MenuInfoServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    @Bean(name="wxPosOrderServiceFacade")
    public ReferenceBean<WxPosOrderServiceFacade> wxPosOrderServiceFacade() {
        ReferenceBean<WxPosOrderServiceFacade> ref = new ReferenceBean<WxPosOrderServiceFacade>();
        ref.setInterface(WxPosOrderServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    
    
    @Bean(name="enterPrisePaymentServiceFacade")
    public ReferenceBean<EnterPrisePaymentServiceFacade> enterPrisePaymentServiceFacade() {
        ReferenceBean<EnterPrisePaymentServiceFacade> ref = new ReferenceBean<EnterPrisePaymentServiceFacade>();
        ref.setInterface(EnterPrisePaymentServiceFacade.class);
        ref.setTimeout(5000);
        ref.setRegistry(payRegConfig());
        ref.setCheck(false);
        return ref;
    }
    

}
